package com.open.capacity.finance.service.impl;

import com.open.capacity.finance.dao.AuditModelDao;
import com.open.capacity.finance.dto.echart.AuditWarnDto;
import com.open.capacity.finance.entity.AuditModel;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.utils.SysUserUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.finance.entity.AuditModelWarnUser;
import com.open.capacity.finance.dao.AuditModelWarnUserDao;
import com.open.capacity.finance.service.AuditModelWarnUserService;


@Service
public class AuditModelWarnUserServiceImpl implements AuditModelWarnUserService {


    @Autowired
    private AuditModelWarnUserDao auditModelWarnDao;

    @Autowired
    private AuditModelDao auditModelDao;

    /**
     * 添加
     *
     * @param auditModelWarn
     */
    public int save(AuditModelWarnUser auditModelWarn) {
        return auditModelWarnDao.save(auditModelWarn);
    }

    /**
     * 修改
     *
     * @param auditModelWarn
     */
    public int update(AuditModelWarnUser auditModelWarn) {
        return auditModelWarnDao.update(auditModelWarn);
    }

    @Override
    public int read(Long id) {
        return auditModelWarnDao.updateReadFlag(id);
    }

    @Override
    public int ingore(Long id, Long day) {
        return auditModelWarnDao.updateIngoreFlag(id, DateUtil.addDay(new Date(), day));
    }


    /**
     * 删除
     *
     * @param warnId
     */
    public int deleteByWarnId(Long warnId) {
        return auditModelWarnDao.deleteByWarnId(warnId);
    }


    @Override
    public AuditModelWarnUser findLastByModelId(String modelId, String companyId) {
        Map<String, Object> param = new HashMap<>();
        param.put("modelId", modelId);
        param.put("companyId", companyId);
        List<AuditModelWarnUser> list = auditModelWarnDao.findAll(param);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public AuditWarnDto countWarn() {
        SysUser loginUser = SysUserUtil.getLoginAppUser();
        Map<String, Object> param = new HashMap<>();
        param.put("status", 1);
        param.put("companyId", loginUser.getCompanyId());
        List<AuditModel> modelList = auditModelDao.findAll(param);
        List<AuditModelWarnUser> warnList = auditModelWarnDao.findByCompanyId(loginUser.getCompanyId());

        AuditWarnDto dto = new AuditWarnDto();
        dto.setTotalCount(modelList.size());

        dto.setNomalCount(modelList.size() - warnList.size());
        dto.setErrorCount(warnList.size());
        dto.setDealDoingCount(warnList.stream().filter(t -> t.getDealFlag().equals(1)).count());
        dto.setDealCompleteCount(warnList.stream().filter(t -> t.getDealFlag().equals(2)).count());
        return dto;
    }

    @Override
    public boolean needWarn(String modelId, String companyId, String userId) {
        AuditModelWarnUser userNeedWarnDate = auditModelWarnDao.findUserNeedWarnDate(modelId, companyId, userId);
        if (userNeedWarnDate != null && userNeedWarnDate.getDealEnddate().compareTo(new Date()) > 0) {
            return false;
        }
        return true;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<AuditModelWarnUser> pageMy(Map<String, Object> params) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        params.put("userId", loginUser.getId());

        List<AuditModelWarnUser> list = auditModelWarnDao.findMy(params);

        PageInfo<AuditModelWarnUser> pageInfo = new PageInfo(list);

        return PageResult.<AuditModelWarnUser>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 全部告警
     *
     * @param params
     * @return
     */
    public PageResult<AuditModelWarnUser> pageAll(Map<String, Object> params) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        if (!loginUser.getCompanyId().equals("00")) {
            params.put("userId", loginUser.getId());
        }

        List<AuditModelWarnUser> list = auditModelWarnDao.findAll(params);

        PageInfo<AuditModelWarnUser> pageInfo = new PageInfo(list);

        return PageResult.<AuditModelWarnUser>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<AuditModelWarnUser> findAll(Map<String, Object> params) {
        return auditModelWarnDao.findAll(params);
    }
}
